package pro.lingwu.station.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pro.lingwu.station.dao.AdminMapper;
import pro.lingwu.station.dto.PagesResult;
import pro.lingwu.station.dto.db.Admin;
import pro.lingwu.station.service.AdminService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author @lingwu
 * @date created in 11/18/2021
 */
@Service
public class AdminServiceImpl implements AdminService {

    private final AdminMapper mapper;

    public AdminServiceImpl(AdminMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void addAdmin(Admin admin) {
        mapper.addAdmin(admin);
    }

    @Override
    public Admin signIn(String account, String password, HttpServletRequest request) {
        Admin admin = mapper.findAdminBy(account);
        if(password.equals(admin.getPassword())){
            HttpSession session = request.getSession();
            session.setAttribute("admin-sign-in", true);
        }
        return admin;
    }

    @Override
    public void updateAdmin(Admin admin) {
        mapper.updateAdmin(admin);
    }

    @Override
    public PagesResult<Admin> getAdmins(int start, int size) {
        PageHelper.startPage(start, size);
        var res = new PagesResult<Admin>();
        res.setTotal(mapper.getTotal());
        res.setResult(mapper.getAdmins());
        return res;
    }

    @Override
    public void rmAdmin(String account) {
        mapper.rmAdmin(account);
    }
}

